package com.lagou.task12;

public class StringIndexTest {
    public static void main(String[] args) {
        String s1 = new String("Good GOOd study,Day Day UP");

        System.out.println(s1.indexOf('G'));//0
        System.out.println(s1.indexOf('D',10));
        System.out.println(s1.indexOf('g'));//-1

        System.out.println(s1.indexOf("good"));
        System.out.println(s1.indexOf("Good"));
        System.out.println(s1.indexOf("GOOd",5));

        //编写通用代码实现str1中出现的所有位置，并打印出来
        int pos = 0;
        pos = s1.indexOf("Day");
        while (-1 != pos){
            System.out.println("pos = " + pos);
            pos = s1.indexOf("Day",pos+1);
        }
        System.out.println("----------------------------------");
        //优化
        pos = 0;
        while ((pos = s1.indexOf("Day",pos)) != -1){
            System.out.println("pos = " + pos);
            pos += "Day".length();
        }

        //实现字符串的反向查找
        System.out.println(s1.lastIndexOf('G'));//5
        System.out.println(s1.lastIndexOf('G',4));//0
        System.out.println(s1.lastIndexOf('G',6));//5

        //字符串截取
        String s2 = new String("abcdefg");
        //包含起始位置，不包含结束位置
        System.out.println(s2.substring(0,6));//abcdef
        System.out.println(s2.substring(0,7));//abcdefg
        //包含起始位置，
        System.out.println(s2.substring(3));
    }
}
